package cn.wxxyhw.server;

import cn.wxxyhw.Dao.ArticleDao;
import cn.wxxyhw.Dao.CommentDao;
import cn.wxxyhw.Dao.LiuyanDao;
import cn.wxxyhw.Dao.UserDao;
import cn.wxxyhw.Entity.Comment;
import cn.wxxyhw.Util.DateFormateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: 洪稳
 * @Date: 2019/6/5
 */
@Service
public class CommentServer {

    @Autowired
    private UserDao userDao;
    @Autowired
    private CommentDao commentDao;
    /**
     * 保存评论
     * @param id
     * @param fromName
     * @param toName
     * @param desc
     */
    public void savePL(String id, String fromName, String toName, String desc,String fromId,String toId) {
        if (!"".equals(toName)){
            desc ="<span style='color:red;' >回复 "+toName+" : </span>"+desc;
        }
        Comment comment = new Comment();
        comment.setLiuyanId(Integer.parseInt(id));
        comment.setArticleId(0); //表示不是说说
        comment.setFromName(fromName);
        comment.setToName(toName);
        comment.setContent(desc);
        comment.setFromId(fromId);
        comment.setToId(toId);
        comment.setDate(DateFormateUtil.getDateFormat().format(new Date()));
        comment.setFromImage(userDao.getImageByName(fromId));
        commentDao.save(comment);
    }

    /**
     * 得到全部评论
     * @return
     */
    public List<Comment> getAllPL(){
        List<Comment> list  = commentDao.getAll();
        return list;
    }

    /**
     * 删除评论
     * @param id 评论id
     */
    public void del(String id) {
        commentDao.del(Integer.parseInt(id));
    }

    /**
     * 保存说说评论
     * @param articleId
     * @param desc
     * @param name
     */
    public void savePL(int articleId, String desc, String name,String toName,String d,String fromid,String toId) {
        if (!"".equals(toName)){
            desc="<span style='color:red;'>回复 "+toName+" : </span>"+desc;
        }
        Comment comment = new Comment();
        comment.setFromName(name);
        comment.setFromImage(userDao.getImageByName(fromid));
        comment.setToName(toName);
        comment.setDate(DateFormateUtil.getDateFormat().format(new Date()));
        comment.setArticleId(articleId);
        comment.setContent(desc);
        comment.setToId(toId);
        comment.setFromId(fromid);
        commentDao.save(comment);
    }

    /**
     * 根据留言id删除评论
     * @param id
     */
    public void delByLyId(String id ,int aid) {
        commentDao.delByLyId(Integer.parseInt(id),aid);
    }
}
